/*
 * Licensed to the Tai Ping Jin Ke
 *
 * Copyright (c) 2022 .
 * All rights reserved.
 * 项目名称：太平金科-基础组件封装层-SpringCloud组件封装-缺省实现
 * 版权说明：本软件属太平金融科技服务(上海)有限公司所有，在未获得太平金融科技服务(上海)有限公司正式授权情况下，任何企业和个人，不能获取、阅读、安装、传播本软件涉及的任何受知识产权保护的内容。
 */
package com.taipingframework.cloud.web.listener;

import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.cloud.nacos.NacosConfigProperties;
import com.alibaba.nacos.api.config.ConfigChangeEvent;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.client.config.listener.impl.AbstractConfigChangeListener;
import com.taipingframework.cloud.web.support.EnvironmentChangedEvent;
import com.taipingframework.utility.context.BeanLifecycle;
import com.taipingframework.utility.context.SpringContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Component
public class NacosListenerConfig extends BeanLifecycle {

    private static ApplicationContext applicationContext;

    @Resource
    private NacosConfigManager nacosConfigManager;

    // private static final String DATA_ID = "potyun-authorize-biz.yaml";
    // private static final String GROUP = "potyun.com";

    @Override
    protected void initMethod() {
        try {
            applicationContext = SpringContextHolder.getApplicationContext();

            ConfigService configService = nacosConfigManager.getConfigService();
            NacosConfigProperties nacosConfigProperties = nacosConfigManager.getNacosConfigProperties();
            ConfigListener configListener = new ConfigListener();

            List<NacosConfigProperties.Config> sharedConfigs = nacosConfigProperties.getSharedConfigs();
            for (NacosConfigProperties.Config config : sharedConfigs) {
                configService.addListener(config.getDataId(), config.getGroup(), configListener);
            }

            List<NacosConfigProperties.Config> extensionConfigs = nacosConfigProperties.getExtensionConfigs();
            for (NacosConfigProperties.Config config : extensionConfigs) {
                configService.addListener(config.getDataId(), config.getGroup(), configListener);
            }
            log.debug("Nacos配置变更监听事件通知已开启");
/*
            // 获取Nacos配置
            String configContent = configService.getConfig(DATA_ID, GROUP, Duration.ofSeconds(5).get(ChronoUnit.MILLIS));
            // 加载nacos配置
            Yaml yaml = new Yaml();
            Properties properties = yaml.loadAs(configContent, Properties.class);
            // 编辑nacos配置
            properties.put("cfg-key", "cfg-val");
            configContent = yaml.dump(properties);
            // 发布nacos配置
            configService.publishConfig(DATA_ID, GROUP, configContent);
*/
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    static class ConfigListener extends AbstractConfigChangeListener {
        @Override
        public void receiveConfigChange(ConfigChangeEvent event) {
            applicationContext.publishEvent(new EnvironmentChangedEvent(this, event));
        }
    }

}
